package com.example.shixun.controller;

import com.example.shixun.pojo.Result;
import com.example.shixun.pojo.Student;
import com.example.shixun.service.StudentService;
import com.example.shixun.utils.Md5;
import com.example.shixun.utils.PageInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@Controller
@ResponseBody
@RequestMapping("/student")
public class StudentController {
    @Resource
    StudentService studentService;

    Md5 md5=new Md5();
    @GetMapping("/lists")
    public Result getStudentList(Integer pageNum,Integer pageSize,Student student){
//        Student stu=new Student();
//        return  studentService.getStudentList(stu);
        System.out.println("学生姓名:"+student.getUserName());
        System.out.println("学生性别:"+student.getSex());
        PageInfoUtil<Student> pageInfoUtil =new PageInfoUtil<Student>();
        pageInfoUtil.setPageNum(pageNum);
        pageInfoUtil.setPageSize(pageSize);
        System.out.println(pageSize);
        pageInfoUtil.setParams(student);
        PageInfoUtil<Student> pageInfo= this.studentService.getStudentList(pageInfoUtil);
        return  Result.success(pageInfo);
    }
    @PostMapping("/regist")
    public Result<Integer> regist(Student student){
        student.setPwd(md5.code(student.getPwd()));
        return studentService.AddStudent(student);
    }

    @DeleteMapping("/delete/{id}")
    public Result<Integer> deleteById(@PathVariable Integer id){
        log.info("删除学生");
        if(id!=null){
            studentService.deleteById(id);
            return Result.success(1);
        }else{
            return Result.fail("id不存在");
        }
    }

}
